package com.william.lifetraxer;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.widget.Toast;

import com.william.lifetraxer.utils.HttpHelper;

public class DuringLogin extends BasicActivity implements Runnable{
	
	Handler mHandler = new Handler(){

			@Override
			public void handleMessage(Message msg) {
				Toast.makeText(DuringLogin.this, (String)msg.obj, 4000).show();
				super.handleMessage(msg);
			}
			
		};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
				WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

		setContentView(R.layout.during_login);
		new Thread(this).start();
	}

	@Override
	public void run() {
		
		try{
			Thread.sleep(4000);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		String result = login();
		
		if ("LOGIN_SUCCESS".equals(result)) {
			Message msg = mHandler.obtainMessage();
			msg.obj= DuringLogin.this.getResources().getString(R.string.during_login_textview_login_sucess_text);
			msg.sendToTarget();
			startActivity(new Intent(this, MainPage.class));
			DuringLogin.this.finish();
		} else if ("LOGIN_FAILED".equals(result)) {
			Message msg = mHandler.obtainMessage();
			msg.obj= DuringLogin.this.getResources().getString(R.string.during_login_textview_login_failed_text);
			msg.sendToTarget();
			DuringLogin.this.finish();
		} else if (result == "DATABASE_ERROR") {
			Message msg = mHandler.obtainMessage();
			msg.obj= DuringLogin.this.getResources().getString(R.string.during_login_textview_login_failed_text);
			msg.sendToTarget();
			DuringLogin.this.finish();
		}
	}

	private String login() {
		Intent intent = getIntent();
		Bundle bundle = intent.getBundleExtra("fromLogin");
		String username = bundle.getString("username");
		String password = bundle.getString("password");

		String result = HttpHelper.login(username, password);
		return result;
	}

	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK&&event.getRepeatCount()==0){
			
		}
		return false;
	}

}